package peishui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.xkh.xkh.R;

import java.util.ArrayList;

import com.xkh.xkh.MyApplication;
import managers.UIHandler;
import peishui.fragments.CannelDialogFragment;
import peishui.network.http.GetRealCannelDam;
import peishui.views.adapters.GateAdapter;
import shuiyuqing.model.BasicInfoModel;
import shuiyuqing.network.beans.IrrBAWaterGate;
import shuiyuqing.network.beans.IrrBTCanalDetail;
import shuiyuqing.network.beans.RealTimeIrrBRWaterDam;

/**
 * 配水界面
 * Created by 凯 on 2015/11/10.
 */
public class PeiShuiActivity extends UIHandler.BaseActivity implements peishui.fragments.CannelDialogFragment.Delegate{

    // 返回
    Button btn_back;

    //　选择渠道
    View v_sel_cannel;

    // 选择渠道名称
    TextView tv_cannalName;

    // 选择渠道
    IrrBTCanalDetail canalDetail;

    // 刷新显示的界面
    View v_refresh;

    // 渠道对应的闸门信息
    ListView lv_gatedams;

    // 手势
    GestureDetector mGestureDetector;

    // 数据适配器
    GateAdapter adapter;

    public GateAdapter getAdapter() {
        return adapter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_peishui);
        initview();

        initviewwithdata();
    }

    // 初始化界面数据
    private void initviewwithdata() {
        IrrBTCanalDetail irrBTCanalDetail = BasicInfoModel.getCanalDetail("CT01000000");
        if (irrBTCanalDetail != null)
        {
            select(irrBTCanalDetail);
        }
    }

    View.OnClickListener itemClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == v_sel_cannel)
            {
                CannelDialogFragment dialogFragment = new CannelDialogFragment();
                dialogFragment.show(getFragmentManager(),CannelDialogFragment.class.getSimpleName());
            }
            else if (v == btn_back)
            {
                finish();
            }
        }
    };


    // 初始化view
    private void initview() {
        // 返回按钮
        btn_back = (Button) findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(itemClick);

        v_sel_cannel = findViewById(R.id.id_v_sel_cannel);
        v_sel_cannel.setOnClickListener(itemClick);

        tv_cannalName = (TextView) findViewById(R.id.id_tv_cannelName);
        tv_cannalName.setOnClickListener(itemClick);

        lv_gatedams = (ListView) findViewById(R.id.id_lv_gateDam);

        v_refresh = findViewById(R.id.id_v_refresh);



        // 监听手势，进行处理
        mGestureDetector = new GestureDetector(MyApplication.share(),new GestureDetector.SimpleOnGestureListener()
        {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                if (Math.abs(velocityY) < 100) {
                    // System.out.println("手指移动的太慢了");
                    return true;
                }

                // 手势向下 down
                if ((e2.getRawY() - e1.getRawY()) > 200) {
                    System.out.println("向下");
                    v_refresh.setVisibility(View.VISIBLE);
                    onRefresh();
                    return true;
                }
                // 手势向上 up
                if ((e1.getRawY() - e2.getRawY()) > 200) {
                    return true;
                }

                return super.onFling(e1, e2, velocityX, velocityY);
            }
        });
    }

    // 刷新动作
    private void onRefresh() {
        v_refresh.setVisibility(View.VISIBLE);
        UIHandler.uiHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                v_refresh.setVisibility(View.GONE);
            }
        },1000);

        if (adapter != null)
        {
            ArrayList<String> stadias = new ArrayList<String>();
            // 所有的闸门信息
            ArrayList<IrrBAWaterGate> watergates = adapter.getWaterGates();
            for (IrrBAWaterGate g:watergates)
            {
                stadias.add(g.getStadiaCode());
            }

            // 获取实时数据
            new GetRealCannelDam(new GetRealCannelDam.Upload(stadias)).get();
        }

    }


    // 注册手势
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    // 选择某渠道
    @Override
    public void select(IrrBTCanalDetail canalDetail) {
        tv_cannalName.setText(canalDetail.getCanalName());
        this.canalDetail = canalDetail;

        adapter = new GateAdapter();
        adapter.setCanalDetail(canalDetail);
        lv_gatedams.setAdapter(adapter);

        ArrayList<String> stadias = new ArrayList<String>();
        // 所有的闸门信息
        ArrayList<IrrBAWaterGate> watergates = adapter.getWaterGates();
        for (IrrBAWaterGate g:watergates)
        {
            stadias.add(g.getStadiaCode());
        }

        // 获取实时数据
        new GetRealCannelDam(new GetRealCannelDam.Upload(stadias)).get();

        lv_gatedams.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                IrrBAWaterGate gate = (IrrBAWaterGate) view.getTag(R.id.id_gate);
                RealTimeIrrBRWaterDam dam = (RealTimeIrrBRWaterDam) view.getTag(R.id.id_gateDam);

                Intent intent = new Intent(PeiShuiActivity.this,GateActivity.class);
                intent.putExtra("gate",gate);
                intent.putExtra("dam",dam);

                startActivity(intent);
            }
        });

    }
}
